/*
 * Input driver for resistor ladder connected on ADC
 *
 * Copyright (c) 2016 Alexandre Belloni
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

#include <linux/err.h>
#include <linux/iio/consumer.h>
#include <linux/iio/types.h>
#include <linux/iio/iio.h>
#include <linux/input.h>
#include <linux/input-polldev.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/adc_keys.h>

#define FH_ADC_POLL_INTERVAL (120)
extern struct iio_chan_spec * fh_adckey_iio_channel_get(void);
extern int fh_iio_read_channel_raw(struct iio_chan_spec *chan, int *val);

struct adc_keys_state {
	struct iio_channel *channel;
	struct iio_chan_spec * chan_spec;
	u32 num_keys;
	u32 last_key;
	u32 keyup_voltage;
	const struct adc_keys_button *map;
};

static int g_last_value = 0;
static void adc_keys_poll(struct input_polled_dev *dev)
{
	struct adc_keys_state *st = dev->private;
	int i, value, ret;
	u32 diff, closest = 0xffffffff;
	int keycode = 0;

	ret = fh_iio_read_channel_raw(st->chan_spec, &value);
	if (unlikely(ret < 0)) {
		/* Forcibly release key if any was pressed */
		value = st->keyup_voltage;
	} else {
		for (i = 0; i < st->num_keys; i++) {
			diff = abs(st->map[i].voltage - value);
			if (diff < closest) {
				closest = diff;
				keycode = st->map[i].keycode;
			}
		}
	}
	if(abs(g_last_value - value) > 3) {
		printk("adc_keys_poll:volt:%d, code:%d,diff:%d,closet:%d,lask_key:%d\n", value, keycode, diff, closest, st->last_key);
	}
	g_last_value = value;

	if (abs(st->keyup_voltage - value) < closest)
		keycode = 0;

	if (st->last_key && st->last_key != keycode) {
		input_report_key(dev->input, st->last_key, 0);
		printk("adc_keys_poll: key_release%d\n", st->last_key);
	}

	if (keycode) {
		input_report_key(dev->input, keycode, 1);
		printk("adc_keys_poll: key_press:%d\n", keycode);
	}

	input_sync(dev->input);
	st->last_key = keycode;
}

static int adc_keys_load_keymap(struct device *dev, struct adc_keys_state *st)
{
    struct adc_keys_platform_data *pdata = dev_get_platdata(dev);
    if (!pdata) {
        dev_err(dev, "No platform data available\n");
        return -EINVAL;
    }

    st->num_keys = pdata->nbuttons;
    st->map = pdata->buttons;
    st->keyup_voltage = pdata->reference_voltage;

    return 0;

}

static int adc_keys_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct adc_keys_state *st;
	struct input_polled_dev *poll_dev;
	struct input_dev *input;
	enum iio_chan_type type;
	int i, value;
	int error;

	dev_err(dev, "adc_keys_probe enter\n");
	st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
	if (!st)
		return -ENOMEM;

	st->chan_spec = fh_adckey_iio_channel_get();
	if (IS_ERR(st->chan_spec))
		return PTR_ERR(st->chan_spec);

	error = adc_keys_load_keymap(dev, st);
	if (error)
		return error;

	platform_set_drvdata(pdev, st);

	poll_dev = devm_input_allocate_polled_device(dev);
	if (!poll_dev) {
		dev_err(dev, "failed to allocate input device\n");
		return -ENOMEM;
	}

	 if (!device_property_read_u32(dev, "poll-interval", &value))
	 	poll_dev->poll_interval = value;
	 else
	    poll_dev->poll_interval = FH_ADC_POLL_INTERVAL;

	poll_dev->poll = adc_keys_poll;
	poll_dev->private = st;

	input = poll_dev->input;

	input->phys = "adc-keys/input0";

	input->id.bustype = BUS_HOST;
	input->id.vendor = 0x0001;
	input->id.product = 0x0001;
	input->id.version = 0x0100;

	__set_bit(EV_KEY, input->evbit);
	for (i = 0; i < st->num_keys; i++)
		__set_bit(st->map[i].keycode, input->keybit);

	if (device_property_read_bool(dev, "autorepeat"))
		__set_bit(EV_REP, input->evbit);

	error = input_register_polled_device(poll_dev);
	if (error) {
		dev_err(dev, "Unable to register input device: %d\n", error);
		return error;
	}
	dev_err(dev, "adc_keys_probe success\n");
	return 0;
}

#ifdef CONFIG_OF
static const struct of_device_id adc_keys_of_match[] = {
	{ .compatible = "adc-keys", },
	{ }
};
MODULE_DEVICE_TABLE(of, adc_keys_of_match);
else
static struct of_device_id const fh_sadc_of_match[] = {
	{ .compatible = "adc_keys" },
	{}
};
MODULE_DEVICE_TABLE(of, fh_adc_key_of_match);
#endif

static struct platform_driver __refdata adc_keys_driver = {
	.driver = {
		.name = "adc_keys",
		.owner	= THIS_MODULE,
		.of_match_table = of_match_ptr(adc_keys_of_match),
	},
	.probe = adc_keys_probe,
};
module_platform_driver(adc_keys_driver);

MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
MODULE_DESCRIPTION("Input driver for resistor ladder connected on ADC");
MODULE_LICENSE("GPL v2");
